﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronSuit.DataMap;
using System.Threading;
using IronSuit.Utility;
using IronSuit.Crazy250API.BaseLogic;

namespace IronSuit.Crazy250API.UserAPI
{
	public static class UserThread
	{
		static Thread userThread;
		static Queue<DataQueueHandler> EntityQueue = new Queue<DataQueueHandler>();
		static UserThread()
		{
			userThread = new Thread(UserDataLogin);
			userThread.IsBackground = true;
			userThread.Start();
		}
		public static void InserQueue(DataEntity entity, DataHandleType handleType)
		{
			DataQueueHandler dh = new DataQueueHandler(entity, handleType);
			EntityQueue.Enqueue(dh);
		}
		static void UserDataLogin()
		{
			DBFactory DBF=null;
			while (true)
			{
				try
				{
					DBF = new DBFactory("UserConn");

					while (EntityQueue.Count > 0)
					{
						DataQueueHandler dh = EntityQueue.Dequeue();
						switch (dh.HandleType)
						{
							case DataHandleType.Insert:
								DBF.Insert(dh.Entity);
								break;
							case DataHandleType.Update:
								DBF.Update(dh.Entity);
								break;
							case DataHandleType.Delete:
								DBF.Delete(dh.Entity);
								break;
							default:
								break;
						}
					}
				}
				catch (Exception e)
				{
					LogHelper.Fatal("UserDataThread", e.Message, e);
				}
				finally
				{
					if (DBF != null)
						DBF.Close();
				}
				Thread.Sleep(50);
			}
		}
	}
	
}
